package com.chaoxuan.uebt.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.adapter.StoreAdapter;
import com.chaoxuan.uebt.model.News;
import com.chaoxuan.uebt.model.Store;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class StoreActivity extends BaseActivity implements View.OnClickListener,OnRefreshListener,OnLoadMoreListener,BaseQuickAdapter.OnItemClickListener {
    StoreAdapter adapter;
    int page;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);

        initTitle();

        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(gridLayoutManager);
        SmartRefreshLayout refreshLayout = (SmartRefreshLayout)findViewById(R.id.refreshLayout);

        adapter= new StoreAdapter();
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        adapter.setOnItemClickListener(this);
        refreshLayout.autoRefresh();
        findViewById(R.id.btn_confirm).setOnClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Store store=(Store) adapter.getData().get(position);
        if(store==null)return;
        //1：实物商品；2：虚拟商品
        //store.getType()
        Intent i=new Intent(this,StoreDetailActivity.class);
        i.putExtra("id",store.getId());
        startActivity(i);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page=adapter.getData().size();
        load(false,refreshLayout);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page=0;
        load(true,refreshLayout);
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.btn_confirm){
            Toast.makeText(this,"即将开放，敬请期待！",Toast.LENGTH_LONG).show();
        }
    }

    private void load(final boolean refresh, final RefreshLayout refreshLayout){
        subscribe(RetrofitUtils.getAPIService().indexStore(page), new BaseSubscriber<List<Store>>() {
            @Override
            public void onSuccess(List<Store> data) {
                Log.d("size:",data.size()+"");
                if(refresh){
                    adapter.replaceData(data);
                    refreshLayout.finishRefresh();
                }else{
                    adapter.addData(data);
                    refreshLayout.finishLoadMore();
                }
            }
        });
    }
}
